package com.gzhryc.common.logging.impls;

import com.gzhryc.common.logging.LogAnalysisUtils;
import com.gzhryc.common.logging.Logger;

import java.io.Serializable;

public class DefaultLogger extends Logger implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private org.apache.log4j.Logger log;

	public DefaultLogger(org.apache.log4j.Logger log) {
		this.log = log;
	}

	public boolean isDebug() {
		return log.isDebugEnabled();
	}

	public void debug(String message, Object... objs) {
		if (log != null) {
			Throwable throwable = null;
			if (objs != null && objs.length > 0) {
				for (Object obj : objs) {
					if (Throwable.class.isInstance(obj)) {
						throwable = Throwable.class.cast(obj);
						break;
					}
				}
			}
			if (throwable != null) {
				log.debug(LogAnalysisUtils.out(message, objs), throwable);
			} else {
				log.debug(LogAnalysisUtils.out(message, objs));
			}
		} else {
			System.out.println("[debug]" + LogAnalysisUtils.out(message, objs));
		}
	}

	public void info(String message, Object... objs) {
		if (log != null) {
			log.info(LogAnalysisUtils.out(message, objs));
		} else {
			System.out.println("[info]" + LogAnalysisUtils.out(message, objs));
		}
	}

	public void error(String message, Object... objs) {
		if (log != null) {
			Throwable throwable = null;
			if (objs != null && objs.length > 0) {
				for (Object obj : objs) {
					if (Throwable.class.isInstance(obj)) {
						throwable = Throwable.class.cast(obj);
						break;
					}
				}
			}
			if (throwable != null) {
				log.error(LogAnalysisUtils.out(message, objs), throwable);
			} else {
				log.error(LogAnalysisUtils.out(message, objs));
			}
		} else {
			System.out.println("[error]" + LogAnalysisUtils.out(message, objs));
		}
	}

	public void warn(String message, Object... objs) {
		if (log != null) {
			Throwable throwable = null;
			if (objs != null && objs.length > 0) {
				for (Object obj : objs) {
					if (Throwable.class.isInstance(obj)) {
						throwable = Throwable.class.cast(obj);
						break;
					}
				}
			}
			if (throwable != null) {
				log.warn(LogAnalysisUtils.out(message, objs), throwable);
			} else {
				log.warn(LogAnalysisUtils.out(message, objs));
			}
		} else {
			System.out.println("[warn]" + LogAnalysisUtils.out(message, objs));
		}
	}
}
